/*
 * Copyright (c) 2015. For Intelligent Group.
 */

package com.intelligent.ispc.client.core.controller;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.intelligent.ispc.client.core.modal.grr.ChoiceItemDto;
import com.intelligent.ispc.client.core.view.global.MainStateBar;
import com.intelligent.ispc.client.event.*;
import com.intelligent.ispc.client.utils.ApplicationBeanUtils;
import com.intelligent.ispc.client.utils.EventBusUtils;
import com.intelligent.ispc.client.utils.UIException;
import com.intelligent.ispc.common.mapper.JsonMapper;
import com.intelligent.ispc.common.utils.ApplicationException;
import com.intelligent.ispc.common.utils.ExceptionMessages;
import com.intelligent.ispc.common.utils.StringUtils;
import com.intelligent.ispc.constant.AppConstant;
import com.intelligent.ispc.constant.ProcessConstant;
import com.intelligent.ispc.core.dto.*;
import com.intelligent.ispc.core.service.FileDataService;
import com.intelligent.ispc.core.service.GrrService;
import com.intelligent.ispc.core.service.SystemService;
import com.intelligent.ispc.utils.executor.ExecutorPool;
import com.intelligent.ispc.utils.executor.ExecutorTask;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by cherry on 2016/8/22.
 */
@Component
public class GrrController {

    private final Logger logger = LoggerFactory.getLogger(GrrController.class);

    @Autowired
    private GrrService grrService;

    @Autowired
    private FileDataService fileDataService;

    @Autowired
    private SystemService systemService;


    private JsonMapper mapper = new JsonMapper();

    /**
     * search grr system config
     *
     * @return grrConfigDto in system
     */
    public GrrConfigDto findGrrConfig() {

        GrrConfigDto grrConfigDto = null;

        try {

            grrConfigDto = systemService.findGrrConfig();
            EventBusUtils.post(new StatusBarMsgChangeEvent("In Analysis/GRR."));
            logger.debug("GrrController findGrrConfig : " + grrConfigDto);
        } catch (ApplicationException e) {

            logger.error("GrrController findGrrConfig error :" + e.getMessage());
            EventBusUtils.post(new ThrowExceptionEvent(e.getFullMessage()));
        }
        return grrConfigDto;
    }

    /**
     * @param keyValueDtos imported files
     * @param dimension    dimension for slot module
     */
    public void importFile(List<KeyValueDto> keyValueDtos, String dimension) {

        logger.debug("GrrController importFile parameter keyValueDtos is : " + keyValueDtos);
        logger.debug("GrrController importFile parameter dimension is: " + dimension);

        try {
            fileDataService.importGrrData(keyValueDtos, dimension);
            EventBusUtils.post(new StatusBarMsgChangeEvent("In Analysis/GRR."));
        } catch (ApplicationException e) {
            EventBusUtils.post(new ThrowExceptionEvent(e.getFullMessage()));
        } catch (Exception e) {
            EventBusUtils.post(new ThrowExceptionEvent(ExceptionMessages.ERR_20001, e.getMessage()));
        }
    }

    /**
     * @param files imported files
     * @return file names is existed
     */
    public List<String> validateFiles(List<KeyValueDto> files) {
        List<String> re = new ArrayList<>();
        try {
            List<String> names = new ArrayList<>();
            for (KeyValueDto dto : files) {
                names.add(dto.getKey());
            }
            List<FileDataDto> results = fileDataService.validateFileName(names, AppConstant.FILE_IMPORT_TYPE_GRR);
            for (FileDataDto dto : results) {
                re.add(dto.getName());
            }
        } catch (ApplicationException e) {
            EventBusUtils.post(new ThrowExceptionEvent(e.getFullMessage()));
        } catch (Exception e) {
            EventBusUtils.post(new ThrowExceptionEvent(ExceptionMessages.ERR_20001, e.getMessage()));
        }

        return re;
    }

    /**
     * search all files
     *
     * @return list files
     */
    public List<FileDataDto> findFiles() {

        List<FileDataDto> fileDataDtos = null;
        try {
            fileDataDtos = fileDataService.findFileData(AppConstant.FILE_IMPORT_TYPE_GRR);
            EventBusUtils.post(new StatusBarMsgChangeEvent("In Analysis/GRR."));
            logger.debug("GrrController findFiles is : " + fileDataDtos);
        } catch (ApplicationException e) {
            logger.error("GrrController findFiles error :" + e.getFullMessage());
            EventBusUtils.post(new ThrowExceptionEvent(e.getFullMessage()));
        } catch (Exception e) {
            logger.error("GrrController findFiles error :" + e.getMessage());
            EventBusUtils.post(new ThrowExceptionEvent(ExceptionMessages.ERR_20001, e.getMessage()));
        }

        return fileDataDtos;
    }

    /**
     * delete file by file object
     *
     * @param fileDataDto file object
     */
    public void deleteFile(FileDataDto fileDataDto) {

        logger.debug("GrrController deleteFile parameter fileDataDto is : " + fileDataDto);

        String name = fileDataDto.getName();
        try {
            fileDataService.deleteFileDataByName(name, AppConstant.FILE_IMPORT_TYPE_GRR);
            EventBusUtils.post(new StatusBarMsgChangeEvent("In Analysis/GRR."));
            logger.debug("GrrController deleteFile success");

        } catch (ApplicationException e) {
            logger.error("GrrController deleteFile error :" + e.getFullMessage());
            EventBusUtils.post(new ThrowExceptionEvent(e.getFullMessage()));
        } catch (Exception e) {
            logger.error("GrrController deleteFile error :" + e.getMessage());
            EventBusUtils.post(new ThrowExceptionEvent(ExceptionMessages.ERR_20001, e.getMessage()));
        }
    }

    /**
     * delete all files type of grr
     */
    public void deleteAllFiles() {

        try {

            fileDataService.deleteAllFileDatas(AppConstant.FILE_IMPORT_TYPE_GRR);
            EventBusUtils.post(new StatusBarMsgChangeEvent("In Analysis/GRR."));
            logger.debug("GrrController deleteAllFiles success");

        } catch (ApplicationException e) {

            logger.error("GrrController deleteAllFiles error :" + e.getFullMessage());
            EventBusUtils.post(new ThrowExceptionEvent(e.getFullMessage()));
        } catch (Exception e) {
            logger.error("GrrController deleteAllFiles error :" + e.getMessage());
            EventBusUtils.post(new ThrowExceptionEvent(ExceptionMessages.ERR_20001, e.getMessage()));
        }
    }

    /**
     * reload files
     */
    public void reloadFile() {

        try {

            fileDataService.reloadData(AppConstant.FILE_IMPORT_TYPE_GRR);
            EventBusUtils.post(new StatusBarMsgChangeEvent("In Analysis/GRR."));
            logger.debug("GrrController reloadFile success");

        } catch (ApplicationException e) {

            logger.error("GrrController reloadFile error :" + e.getFullMessage());
            EventBusUtils.post(new ThrowExceptionEvent(e.getFullMessage()));
        } catch (Exception e) {
            logger.error("GrrController reloadFile error :" + e.getMessage());
            EventBusUtils.post(new ThrowExceptionEvent(ExceptionMessages.ERR_20001, e.getMessage()));
        }
    }

    /**
     * update file status
     *
     * @param fileDataDto selected file object
     */
    public void updateFileData(FileDataDto fileDataDto) {

        logger.debug("GrrController updateFileData parameter fileDataDto is : " + fileDataDto);
        try {
            fileDataService.updateGrrFileData(fileDataDto);
            EventBusUtils.post(new StatusBarMsgChangeEvent("In Analysis/GRR."));
            logger.debug("GrrController updateFileData success");

        } catch (ApplicationException e) {
            logger.error("GrrController updateFileData error :" + e.getFullMessage());
            EventBusUtils.post(new ThrowExceptionEvent(e.getFullMessage()));
        } catch (Exception e) {
            logger.error("GrrController updateFileData error :" + e.getMessage());
            EventBusUtils.post(new ThrowExceptionEvent(ExceptionMessages.ERR_20001, e.getMessage()));
        }
    }

    /**
     * search group by selected file object
     *
     * @param fileDataDto selected file object
     */
    public void findGroup(FileDataDto fileDataDto) {

        try {
            logger.debug("GrrController findGroup parameter fileDataDto is : " + fileDataDto);

            boolean isWorking = ApplicationBeanUtils.getBean(MainStateBar.class).getProcessPanel().isWorking(ProcessConstant.GRR_DECODE_FILE);
            if (isWorking) {
                throw new UIException("This task is already working now.");
            }

            EventBusUtils.post(new StatusBarProcessCreateTaskEvent(ProcessConstant.GRR_DECODE_FILE));

            ExecutorTask task = new ExecutorTask(ProcessConstant.GRR_DECODE_FILE, new ExecutorTask.Work() {
                @Override
                public void execute() {
                    try {
                        GrrGroupDto grrGroupDto = grrService.searchGroupSearchKeys(fileDataDto.getSavePath());

                        if (!ApplicationBeanUtils.getBean(MainStateBar.class).getProcessPanel().isProcessTaskEmpty()) {
                            EventBusUtils.post(new ExecutorTaskFinishEvent(ProcessConstant.GRR_DECODE_FILE, grrGroupDto));
                            EventBusUtils.post(new StatusBarProcessFinishTaskEvent(ProcessConstant.GRR_DECODE_FILE, false));
                        }

                    } catch (ApplicationException e) {
                        EventBusUtils.post(new StatusBarProcessChangeEvent(ProcessConstant.GRR_DECODE_FILE, e.getFullMessage()).level(ProcessConstant.ERROR));
                    }

                }

                @Override
                public void stop() {

                }
            });
            ExecutorPool.submit(task);

        } catch (ApplicationException e) {

            logger.error("GrrController findGroup error :" + e.getFullMessage());
            EventBusUtils.post(new ThrowExceptionEvent(e.getFullMessage()));
        } catch (Exception e) {
            logger.error("GrrController findGroup error :" + e.getMessage());
            EventBusUtils.post(new ThrowExceptionEvent(ExceptionMessages.ERR_20001, e.getMessage()));
        }
    }

    /**
     * search dimension by file name in slot module
     *
     * @param fileName current selected file name
     * @return grrItemDto object
     */
    public GrrItemDto findDimensionByFileName(String fileName) {

        logger.debug("GrrController findDimensionByFileName parameter fileName is : " + fileName);

        GrrItemDto grrItemDto = null;

        try {

            grrItemDto = systemService.findGrrItemByGrrName(fileName);
            EventBusUtils.post(new StatusBarMsgChangeEvent("In Analysis/GRR."));
            logger.debug("GrrController findDimensionByFileName success");

        } catch (ApplicationException e) {

            logger.error("GrrController findDimensionByFileName error :" + e.getFullMessage());
            EventBusUtils.post(new ThrowExceptionEvent(e.getFullMessage()));
        } catch (Exception e) {
            logger.error("GrrController findDimensionByFileName error :" + e.getMessage());
            EventBusUtils.post(new ThrowExceptionEvent(ExceptionMessages.ERR_20001, e.getMessage()));
        }

        return grrItemDto;
    }

    /**
     * update grr item by dimension in slot module
     *
     * @param fileName  current selected file name
     * @param dimension current file's dimension
     */
    public void updateGrrItemByDimension(String fileName, String dimension) {

        logger.debug("GrrController updateGrrItemByDimension parameter fileName is : " + fileName);
        logger.debug("GrrController updateGrrItemByDimension parameter dimension is : " + dimension);

        GrrItemDto grrItemDto = new GrrItemDto();
        grrItemDto.setDimension(dimension);
        grrItemDto.setGrrName(fileName);

        try {
            systemService.updateGrrItem(grrItemDto);
            EventBusUtils.post(new StatusBarMsgChangeEvent("In Analysis/GRR."));
            logger.debug("GrrController updateGrrItemByDimension success");
        } catch (ApplicationException e) {
            logger.error("GrrController updateGrrItemByDimension error :" + e.getFullMessage());
            EventBusUtils.post(new ThrowExceptionEvent(e.getFullMessage()));
        } catch (Exception e) {
            logger.error("GrrController updateGrrItemByDimension error :" + e.getMessage());
            EventBusUtils.post(new ThrowExceptionEvent(ExceptionMessages.ERR_20001, e.getMessage()));
        }
    }

    /**
     * update grr item by itemNames
     *
     * @param grrParamDto grr params
     * @param data        selected lists for items
     */
    public void updateGrrItemByItemNames(GrrParamDto grrParamDto, List<ChoiceItemDto> data) {
        logger.debug("GrrController updateGrrItemByItemNames parameter grrParamDto is : " + grrParamDto);
        logger.debug("GrrController updateGrrItemByItemNames parameter data is : " + data);
        String fileName = "";
        if (grrParamDto != null) {
            fileName = grrParamDto.getGrrName();
        }

        Map<String, Object> map = Maps.newLinkedHashMap();
        for (int i = 0; i < data.size(); i++) {
            map.put(data.get(i).getItemName(), data.get(i).getViewCheck());
        }
        String testItemNames = mapper.toJson(map);
        GrrItemDto grrItemDto = new GrrItemDto();
        grrItemDto.setTestItemNames(testItemNames);
        grrItemDto.setGrrName(fileName);
        try {
            systemService.updateGrrItem(grrItemDto);
        } catch (ApplicationException e) {
            logger.error("GrrController updateGrrItemByItemNames error :" + e.getFullMessage());
            EventBusUtils.post(new ThrowExceptionEvent(e.getFullMessage()));
        } catch (Exception e) {
            logger.error("GrrController updateGrrItemByItemNames error :" + e.getMessage());
            EventBusUtils.post(new ThrowExceptionEvent(ExceptionMessages.ERR_20001, e.getMessage()));
        }
    }

    /**
     * search grr summary data source
     *
     * @param grrParamDto    grr params
     * @param groupSearchKey current selected group object
     * @param startIndex     search start index
     * @param endIndex       search end index
     * @return grr summary lists
     */
    public List<GrrSummaryDto> findGrrSummary(GrrParamDto grrParamDto, GrrGroupDto groupSearchKey, int startIndex, int endIndex) {

        logger.debug("GrrController findGrrSummary parameter grrParamDto is : " + grrParamDto);
        logger.debug("GrrController findGrrSummary parameter groupSearchKey is : " + groupSearchKey);
        logger.debug("GrrController findGrrSummary parameter startIndex is : " + startIndex);
        logger.debug("GrrController findGrrSummary parameter endIndex is : " + endIndex);

        List<GrrSummaryDto> dtos = null;
        try {
            dtos = grrService.searchGrrSummary(grrParamDto, groupSearchKey, startIndex, endIndex);
            EventBusUtils.post(new StatusBarMsgChangeEvent("In Analysis/GRR."));
            logger.debug("GrrController findGrrSummary is " + dtos);
        } catch (ApplicationException e) {
            logger.error("GrrController findGrrSummary error :" + e.getFullMessage());
            EventBusUtils.post(new ThrowExceptionEvent(e.getFullMessage()));
        } catch (Exception e) {
            logger.error("GrrController findGrrSummary error :" + e.getMessage());
            EventBusUtils.post(new ThrowExceptionEvent(ExceptionMessages.ERR_20001, e.getMessage()));
        }

        return dtos;
    }

    /**
     * search grr view data source
     *
     * @param grrParamDto    grr params
     * @param groupSearchKey current selected group object
     * @param startIndex     search start index
     * @param endIndex       search end index
     * @return grr view data lists
     */
    public List<GrrViewDataDto> findGrrViewData(GrrParamDto grrParamDto, GrrGroupDto groupSearchKey, int startIndex, int endIndex) {

        logger.debug("GrrController findGrrViewData parameter grrParamDto is : " + grrParamDto);
        logger.debug("GrrController findGrrViewData parameter groupSearchKey is : " + groupSearchKey);
        logger.debug("GrrController findGrrViewData parameter startIndex is : " + startIndex);
        logger.debug("GrrController findGrrViewData parameter endIndex is : " + endIndex);

        List<GrrViewDataDto> dtos = null;
        try {
            dtos = grrService.searchGrrViewData(grrParamDto, groupSearchKey, startIndex, endIndex);
            EventBusUtils.post(new StatusBarMsgChangeEvent("In Analysis/GRR."));
            logger.debug("GrrController findGrrViewData is " + dtos);
        } catch (ApplicationException e) {
            logger.error("GrrController findGrrViewData error :" + e.getFullMessage());
            EventBusUtils.post(new ThrowExceptionEvent(e.getFullMessage()));
        } catch (Exception e) {
            logger.error("GrrController findGrrViewData error :" + e.getMessage());
            EventBusUtils.post(new ThrowExceptionEvent(ExceptionMessages.ERR_20001, e.getMessage()));
        }

        return dtos;
    }

    /**
     * search grr results
     *
     * @param grrParamDto    grr params
     * @param groupSearchKey current selected group object
     * @param viewDataDtos   checked view data lists
     */
    public void findGrrResult(GrrParamDto grrParamDto, GrrGroupDto groupSearchKey, List<GrrViewDataDto> viewDataDtos) {

        logger.debug("GrrController findGrrResult parameter grrParamDto is : " + grrParamDto);
        logger.debug("GrrController findGrrResult parameter viewDataDtos is : " + viewDataDtos);

        try {
            boolean isWorking = ApplicationBeanUtils.getBean(MainStateBar.class).getProcessPanel().isWorking(ProcessConstant.GRR_SEARCH_SUB_TASK);
            if (isWorking) {
                throw new UIException("This task is already working now.");
            }

            EventBusUtils.post(new StatusBarProcessCreateTaskEvent(ProcessConstant.GRR_SEARCH_SUB_TASK));

            ExecutorTask task = new ExecutorTask(ProcessConstant.GRR_SEARCH_SUB_TASK, new ExecutorTask.Work() {
                @Override
                public void execute() {
                    try {
                        GrrResultDto grrResultDto = grrService.searchGrrResult(grrParamDto, groupSearchKey, viewDataDtos);
                        logger.debug("GrrController findGrrResult success!,  grrViewData size is : " + grrResultDto.getGrrViewDataDtoList().size());

                        if (!ApplicationBeanUtils.getBean(MainStateBar.class).getProcessPanel().isProcessTaskEmpty()) {
                            EventBusUtils.post(new ExecutorTaskFinishEvent(ProcessConstant.GRR_SEARCH_SUB_TASK, grrResultDto));
                            EventBusUtils.post(new StatusBarProcessFinishTaskEvent(ProcessConstant.GRR_SEARCH_SUB_TASK, false));
                        }

                    } catch (ApplicationException e) {
                        EventBusUtils.post(new StatusBarProcessChangeEvent(ProcessConstant.GRR_SEARCH_SUB_TASK, e.getFullMessage()).level(ProcessConstant.ERROR));
                    }
                }

                @Override
                public void stop() {

                }
            });
            ExecutorPool.submit(task);
        } catch (ApplicationException e) {

            EventBusUtils.post(new ThrowExceptionEvent(e.getFullMessage()));
            logger.error("GrrController findGrrResult error :" + e.getFullMessage());
        } catch (Exception e) {
            logger.error("GrrController findGrrResult error :" + e.getMessage());
            EventBusUtils.post(new ThrowExceptionEvent(ExceptionMessages.ERR_20001, e.getMessage()));
        }
    }

    /**
     * update view data
     *
     * @param grrParamDto    grr params
     * @param groupSearchKey current selected group object
     */
    public void updateViewDatas(GrrParamDto grrParamDto, GrrGroupDto groupSearchKey) {

        logger.debug("GrrController updateViewDatas parameter grrParamDto is : " + grrParamDto);
        logger.debug("GrrController updateViewDatas parameter groupSearchKey is : " + groupSearchKey);

        try {
            grrService.updateGrrViewDatas(grrParamDto, groupSearchKey);
            logger.debug("GrrController updateViewDatas success !");

        } catch (ApplicationException e) {

            logger.error("GrrController updateViewDatas error :" + e.getFullMessage());
            EventBusUtils.post(new ThrowExceptionEvent(e.getFullMessage()));
        } catch (Exception e) {
            logger.error("GrrController updateViewDatas error :" + e.getMessage());
            EventBusUtils.post(new ThrowExceptionEvent(ExceptionMessages.ERR_20001, e.getMessage()));
        }
    }

    /**
     * @param grrName current selected grr name
     * @return item lists
     */
    public List<ChoiceItemDto> findItems(String grrName) {

        logger.debug("GrrController findItems parameter grrName is : " + grrName);

        List<ChoiceItemDto> keyValueDtos = null;
        Map<String, Object> values = null;

        try {

            GrrItemDto grrItemDto = systemService.findGrrItemByGrrName(grrName);
            String itemNames = grrItemDto.getTestItemNames();
            if (StringUtils.isBlank(itemNames)) {
                values = Maps.newHashMap();
            } else {
                values = mapper.fromJson(itemNames, mapper.contructMapType(LinkedHashMap.class, String.class, Object.class));
            }

            keyValueDtos = Lists.newLinkedList();

            int index = 1;
            for (Map.Entry<String, Object> entry : values.entrySet()) {
                ChoiceItemDto dto = new ChoiceItemDto();
                dto.setViewCheck((Boolean) entry.getValue());
                dto.setItemName(entry.getKey());
                dto.setIndex(index);
                index++;
                keyValueDtos.add(dto);
            }

            EventBusUtils.post(new StatusBarMsgChangeEvent("In Analysis/GRR."));
            logger.debug("GrrController findItems success, size is : " + keyValueDtos.size());
        } catch (ApplicationException e) {

            logger.error("GrrController findItems error :" + e.getFullMessage());
            EventBusUtils.post(new ThrowExceptionEvent(e.getFullMessage()));
        } catch (Exception e) {
            logger.error("GrrController findItems error :" + e.getMessage());
            EventBusUtils.post(new ThrowExceptionEvent(ExceptionMessages.ERR_20001, e.getMessage()));
        }

        return keyValueDtos;
    }

    /**
     * export summary report
     *
     * @param filePath       current selected file for filePath
     * @param groupSearchKey current selected group object
     * @param itemNames      selected item names
     */
    public void exportSummary(String filePath, GrrGroupDto groupSearchKey, List<String> itemNames) {

        logger.debug("GrrController exportSummary parameter filePath is : " + filePath);
        logger.debug("GrrController exportSummary parameter groupSearchKey is : " + groupSearchKey);

        GrrParamDto grrParamDto = new GrrParamDto();
        grrParamDto.setFilePath(filePath);

        boolean isWorking = ApplicationBeanUtils.getBean(MainStateBar.class).getProcessPanel().isWorking(ProcessConstant.GRR_EXPORT_FILE);
        if (isWorking) {
            throw new UIException("This task is already working now.");
        }
        EventBusUtils.post(new StatusBarProcessCreateTaskEvent(ProcessConstant.GRR_EXPORT_FILE, true));

        ExecutorTask task = new ExecutorTask(ProcessConstant.GRR_EXPORT_FILE, new ExecutorTask.Work() {
            @Override
            public void execute() {
                try {
                    String savePath = grrService.exportSummary(grrParamDto, groupSearchKey, itemNames);
                    logger.debug("GrrController exportSummary success !");

                    if (!ApplicationBeanUtils.getBean(MainStateBar.class).getProcessPanel().isProcessTaskEmpty()) {
                        EventBusUtils.post(new ExecutorTaskFinishEvent(ProcessConstant.GRR_EXPORT_FILE, savePath));
                        EventBusUtils.post(new StatusBarProcessFinishTaskEvent(ProcessConstant.GRR_EXPORT_FILE));
                    }

                } catch (ApplicationException e) {
                    EventBusUtils.post(new StatusBarProcessChangeEvent(ProcessConstant.GRR_EXPORT_FILE, e.getFullMessage()).level(ProcessConstant.ERROR));
                } catch (Exception e) {
                    EventBusUtils.post(new ThrowExceptionEvent(ExceptionMessages.ERR_20001, e.getMessage()));
                }
            }

            @Override
            public void stop() {

            }
        });
        ExecutorPool.submit(task);
    }

    /**
     * export summary and detail report
     *
     * @param filePath       current selected file for filePath
     * @param groupSearchKey current selected group object
     * @param itemNames      selected item names
     */
    public void exportSummaryAndDetail(String filePath, GrrGroupDto groupSearchKey, List<String> itemNames) {

        logger.debug("GrrController exportSummaryAndDetail parameter filePath is : " + filePath);
        logger.debug("GrrController exportSummaryAndDetail parameter groupSearchKey is : " + groupSearchKey);

        GrrParamDto grrParamDto = new GrrParamDto();
        grrParamDto.setFilePath(filePath);

        boolean isWorking = ApplicationBeanUtils.getBean(MainStateBar.class).getProcessPanel().isWorking(ProcessConstant.GRR_EXPORT_FILE);
        if (isWorking) {
            throw new UIException("This task is already working now.");
        }
        EventBusUtils.post(new StatusBarProcessCreateTaskEvent(ProcessConstant.GRR_EXPORT_FILE, true));

        ExecutorTask task = new ExecutorTask(ProcessConstant.GRR_EXPORT_FILE, new ExecutorTask.Work() {
            @Override
            public void execute() {
                try {
                    String savePath = grrService.exportSummaryAndDetail(grrParamDto, groupSearchKey, itemNames);
                    logger.debug("GrrController exportSummaryAndDetail success !");

                    if (!ApplicationBeanUtils.getBean(MainStateBar.class).getProcessPanel().isProcessTaskEmpty()) {
                        EventBusUtils.post(new ExecutorTaskFinishEvent(ProcessConstant.GRR_EXPORT_FILE, savePath));
                        EventBusUtils.post(new StatusBarProcessFinishTaskEvent(ProcessConstant.GRR_EXPORT_FILE));
                    }

                } catch (ApplicationException e) {
                    EventBusUtils.post(new StatusBarProcessChangeEvent(ProcessConstant.GRR_EXPORT_FILE, e.getMessage()).level(ProcessConstant.ERROR));
                } catch (Exception e) {
                    EventBusUtils.post(new ThrowExceptionEvent(ExceptionMessages.ERR_20001, e.getMessage()));
                }
            }

            @Override
            public void stop() {

            }
        });
        ExecutorPool.submit(task);
    }
}

